package adapletplugin.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewItemDialog extends TitleAreaDialog
{
	private String fItem;
	private String fValue;

	private Text fItemText;
	private Text fValueText;

	public NewItemDialog(Shell parentShell)
	{
		super(parentShell);
		fItem = "";
		fValue = "";
	}

	@Override
	public void create()
	{
		super.create();

		setTitle("New Item");
		setMessage("Enter the necessary information to define the new configuration item.");
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		parent.setLayout(new GridLayout());

		Composite body = new Composite(parent, SWT.NULL);
		body.setLayout(new GridLayout(2, false));
		body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label label = new Label(body, SWT.NULL);
		label.setText("Item Name:");

		fItemText = new Text(body, SWT.BORDER);
		fItemText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fItemText.addModifyListener(createModifyListener());

		label = new Label(body, SWT.NULL);
		label.setText("Item Value:");

		fValueText = new Text(body, SWT.BORDER);
		fValueText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fValueText.addModifyListener(createModifyListener());

		return parent;
	}

	private ModifyListener createModifyListener()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (fItemText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (fValueText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else
				{
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};
	}

	@Override
	protected void okPressed()
	{
		fItem = fItemText.getText();
		fValue = fValueText.getText();

		super.okPressed();
	}

	public String getItem()
	{
		return fItem;
	}

	public String getValue()
	{
		return fValue;
	}

}
